﻿using B.S.DataBase.MyProject.Domain.RBAC;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.Menus;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;
using System;
using System.Security.Cryptography;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.Menus
{
    public class GetRoleMenuCommandHandler : IRequestHandler<GetRoleMenuCommand, APIResult<List<RoleMenuDTO>>>
    {
        private readonly IBaseRepository<Role> roleRep;
        private readonly IBaseRepository<RoleMenu> rmRep;
        private readonly IBaseRepository<Menu> menuRep;

        public GetRoleMenuCommandHandler(IBaseRepository<Role> roleRep, IBaseRepository<RoleMenu> rmRep, IBaseRepository<Menu> menuRep)
        {
            this.roleRep = roleRep;
            this.rmRep = rmRep;
            this.menuRep = menuRep;
        }


        public async Task<APIResult<List<RoleMenuDTO>>> Handle(GetRoleMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<RoleMenuDTO>> result = new APIResult<List<RoleMenuDTO>>();

            var list = from a in await roleRep.GetAll()
                       join b in await rmRep.GetAll() on a.RoleId equals b.RoleId
                       join c in await menuRep.GetAll() on b.MenuId equals c.MenuId
                       where (a.IsDeleted == false&&a.RoleId==request.RoleId)
                       select new RoleMenuDTO
                       {
                           MenuId=c.MenuId,
                           MenuName=c.MenuName,
                           Url=c.Url,
                           PId=c.PId,

                           RoleId =a.RoleId,
                           RoleName=a.RoleName,
                           IsDeleted=a.IsDeleted,
                           RMId=b.RMId
                       };
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();
            return await Task.FromResult(result);
        }
    }
}
